package aryn.framework.directory;

import java.io.File;

public abstract class DirHandler {

	private boolean subfolders = false;
	
	public final void openFile(String path, boolean searchSubfolders) {
		this.subfolders = searchSubfolders;
		openFile(path);
	}
	
	public final void openFile(String path) {
		File dir = new File(path);
		if (!dir.isDirectory()) {
			System.out.println(path + " is not a valid directory.");
			System.exit(1);
		}
		onBeforeReading(dir);
		
		String[] list = dir.list();
		for(String file : list) {
			File current = new File(path + File.separator + file);
			onReading(current);
			if (subfolders && current.isDirectory()) {
				openFile(current.getPath());
			}
		}
		
		onAfterReading(dir);
	}
		
	protected abstract void onBeforeReading(File file);
	protected abstract void onReading(File file);
	protected abstract void onAfterReading(File file);
}
